<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form class="layui-form" method="post">
    用户名:<input type="text" name="name" placeholder="请输入用户名" ><br/>
    密  码:<input type="text" name="pass" placeholder="请输入密码"><br/>
    <button >登录</button>
</form>
<script>
    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function(dt){
            $.ajax({
                url:"/user2/login",
                type:"post",
                data:JSON.stringify(dt.field),
                contentType:"application/json",
                dataType:'json',
                success:function(obj) {
                    console.log(obj.code);
                    if(obj.code == 2) {
                        window.location.href="login.html";
                    }else if(obj.code==0){
                        layer.alert(obj.message);
                    }else if (obj.code==1){
                        window.location.href="/index.jsp";
                    }
                },
                error:function(){
                    layer.alert("ajax错误");
                }
            });
            return false;

        });
    });
</script>
</body>
</html>